using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NCS.Library.Helper;
using NCS.Library;
using NCS.Library.Utilities;

namespace NCS.Module.CongTy.Library.Providers
{
    public class SqlDataProvider : DataAccessProvider
    {
        private string connectionString = string.Empty;

        public string ConnectionString
        {
            get { return this.connectionString; }
            set { this.connectionString = value; }
        }

        public SqlDataProvider(string strConn)
        {
            this.ConnectionString = strConn;
        }

        public override DataTable GetCongTyAvailable()
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "spCongTy_GetAllAvailable", AppEnv.GetLanguage(), int.Parse(AppEnv.PortalId()));
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }

        public override DataTable GetAllCongTy()
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "spCongTy_GetAll", AppEnv.GetLanguage(), int.Parse(AppEnv.PortalId()));
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }

        public override DataTable GetAllCongTyFilter(int iStatus)
        {
            DataSet ds = SqlHelper.ExecuteDataset(this.ConnectionString, "spCongTy_GetAllByFilter", AppEnv.GetLanguage(), iStatus, int.Parse(AppEnv.PortalId()));
            if (ds != null && ds.Tables.Count > 0) return ds.Tables[0];
            return null;
        }

        public override CongTyInfo GetInfo(int id)
        {
            IDataReader dr = SqlHelper.ExecuteReader(this.ConnectionString, "spCongTy_GetInfo", id);
            CongTyInfo obj = (CongTyInfo)ObjectHelper.CreateObject(typeof(CongTyInfo), dr);
            return obj;
        }

        public override int SetIndex(int id, int index)
        {
            return SqlHelper.ExecuteNonQuery(this.ConnectionString, "spCongTy_SetIndex", id, index);
        }

        public override int SetStatus(int id, bool status)
        {
            return SqlHelper.ExecuteNonQuery(this.ConnectionString, "spCongTy_SetStatus", id, status);
        }

        public override int CreateCongTy(CongTyInfo objCongTyInfo)
        {
            return (int)SqlHelper.ExecuteScalar(this.ConnectionString, "spCongTy_Insert", 
                                                objCongTyInfo.TieuDe,
                                                objCongTyInfo.NoiDung,
                                                objCongTyInfo.AnhDaiDien, 
                                                objCongTyInfo.TrangThai,
                                                objCongTyInfo.ThuTu,
                                                //objCongTyInfo.CreateDate,
                                                //objCongTyInfo.ModifyDate,
                                                objCongTyInfo.CreateUserID,
                                                //objCongTyInfo.ModifyUserID,
                                                objCongTyInfo.PortalID,
                                                AppEnv.GetLanguage());
        }

        public override int UpdateCongTy(CongTyInfo objCongTyInfo)
        {
            return (int)SqlHelper.ExecuteNonQuery(this.ConnectionString, "spCongTy_Update",
                                               objCongTyInfo.CongTyID,    
                                               objCongTyInfo.TieuDe,
                                               objCongTyInfo.NoiDung,
                                               objCongTyInfo.AnhDaiDien,
                                               objCongTyInfo.TrangThai,
                                               objCongTyInfo.ThuTu,
                                               //objCongTyInfo.ModifyDate,
                                               objCongTyInfo.ModifyUserID,
                                               objCongTyInfo.PortalID,
                                               AppEnv.GetLanguage());
        }

        public override int DeleteCongTY(int id)
        {
            return (int)SqlHelper.ExecuteNonQuery(this.ConnectionString, "spCongTy_Delete", id);
        } 
    }
}
